//
//  HomeFlowServiceCatCell.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/13.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "HomeFlowServiceCatCell.h"
#import "ServiceCategoryCollectionCell.h"

@interface HomeFlowServiceCatCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{
    
    CGSize _cellSize;
    UIEdgeInsets _collectionEdge;
}

@property (nonatomic, strong) UICollectionView *collectionview;

@end

@implementation HomeFlowServiceCatCell

+ (CGFloat)calculateHeight {
    
    CGFloat collectionHeight =  ((TTScreenWidth - 20)/5)*1.2;
    return collectionHeight;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self layout];
    }
    return self;
}

- (void)layout {
    
    [self addSubview:self.collectionview];
    
    [self.collectionview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.bottom.mas_equalTo(0);
    }];
}

- (void)fillCellWithModel:(HomeADItemModel *)model {
        
    self.dataArray = model.sysAdvertisement.sysAdvEventList;
    
    [self.collectionview reloadData];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ServiceCategoryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ServiceCategoryCollectionCellID" forIndexPath:indexPath];
    [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:self.dataArray[indexPath.row].url] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.HomeFlowServiceCatCellBlock) {
        [[StatisticManager shareManager]insertBrowseUserActiveQuantityWithType:@"6"];
        self.HomeFlowServiceCatCellBlock(indexPath.row);
    }
}

- (UICollectionView *)collectionview {
    
    if (!_collectionview) {
        CGFloat width = TTScreenWidth - 20;
        
        CGFloat cellWidth;
        cellWidth = width/5 - 4;
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.minimumLineSpacing = 5;
        
        flowLayout.itemSize = CGSizeMake(cellWidth, cellWidth*1.185);

        _collectionview  = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];

        _collectionview.backgroundColor = [UIColor whiteColor];
        _collectionview.delegate = self;
        _collectionview.dataSource = self;
        _collectionview.pagingEnabled = YES;
        _collectionview.showsHorizontalScrollIndicator = NO;
        _collectionview.showsVerticalScrollIndicator = NO;
        
        [_collectionview registerNib:[UINib nibWithNibName:@"ServiceCategoryCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"ServiceCategoryCollectionCellID"];
    }
    return _collectionview;
}

- (NSMutableArray<HomeADItemEventModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

@end
